﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class NhomKhachDAL:StandardConnectDB,INhomKhach
    {
        public List<DTO.NhomKhachDTO> GetlistNhomKhach()
        {
            List<NhomKhachDTO> listNhomKhach = new List<NhomKhachDTO>();
            DataTable dt = DTExecuteSelect("Select * from NhomKhach");
            foreach (DataRow dr in dt.Rows)
            {
                listNhomKhach.Add(ConvertObjectDataRowToNhomKhachDTO(dr));
            }
            return listNhomKhach;
            
        }

        public DTO.NhomKhachDTO GetNhomKhachByID(string MaNhomKhach)
        {
            NhomKhachDTO nhomkhach = new NhomKhachDTO();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhomKhach", MaNhomKhach);
            DataTable dt = this.DTExecuteSelect("SP_NhomKhach_GetByID", paramlist);
            nhomkhach = ConvertObjectDataRowToNhomKhachDTO(dt.Rows[0]);
            
            return nhomkhach;
            
        }

        public bool InsertNhomKhach(DTO.NhomKhachDTO nhomkhach)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhomKhach", nhomkhach.MaNhomKhach);
            paramlist.Add("@TenNhomKhach", nhomkhach.TenNhomKhach);
            paramlist.Add("@GhiChu", nhomkhach.GhiChu);
            return ExecuteUpdate("SP_NhomKhach_Insert", paramlist);
            
        }

        public bool DeleteNhomKhach(DTO.NhomKhachDTO nhomkhach)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhomKhach", nhomkhach.MaNhomKhach);
            return ExecuteUpdate("SP_NhomKhach_Delete", paramlist);
            
        }

        public bool UpdateNhomKhach(DTO.NhomKhachDTO nhomkhach)
        {
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhomKhach", nhomkhach.MaNhomKhach);
            paramlist.Add("@TenNhomKhach", nhomkhach.TenNhomKhach);
            paramlist.Add("@GhiChu", nhomkhach.GhiChu);
            return ExecuteUpdate("SP_NhomKhach_Update", paramlist);
            
        }

        public DTO.NhomKhachDTO ConvertObjectDataRowToNhomKhachDTO(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                NhomKhachDTO nhomkhach = new NhomKhachDTO();
                nhomkhach.MaNhomKhach = dr["MaNhomKhach"].ToString();
                nhomkhach.TenNhomKhach = dr["TenNhomKhach"].ToString();
                nhomkhach.GhiChu = dr["GhiChu"].ToString();

                return nhomkhach;
            }
            else
                return null;
            
        }
    }
}
